package com.zzf.listviewtest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val fruitList = ArrayList<Fruit>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initFruits()
        val adapter = FruitAdapter(this,R.layout.fruit_item,fruitList)
        listView.adapter = adapter

        listView.setOnItemClickListener { _, _, position, _ ->
            val fruit = fruitList[position]
            Toast.makeText(this, fruit.name, Toast.LENGTH_SHORT).show()

            val toast = Toast.makeText(this, fruit.name, Toast.LENGTH_SHORT).show()


        }
    }

    /*初始化列表数据*/
    private fun initFruits(){
        repeat(2){
            fruitList.add(Fruit("apple",R.mipmap.apple_pic))
            fruitList.add(Fruit("banana_pic",R.mipmap.banana_pic))
            fruitList.add(Fruit("orange_pic",R.mipmap.orange_pic))
            fruitList.add(Fruit("watermelon_pic",R.mipmap.watermelon_pic))
            fruitList.add(Fruit("pear_pic",R.mipmap.pear_pic))
            fruitList.add(Fruit("grape_pic",R.mipmap.grape_pic))
            fruitList.add(Fruit("pineapple_pic",R.mipmap.pineapple_pic))
            fruitList.add(Fruit("strawberry_pic",R.mipmap.strawberry_pic))
            fruitList.add(Fruit("cherry_pic",R.mipmap.cherry_pic))
            fruitList.add(Fruit("mango_pic",R.mipmap.mango_pic))
        }
    }
}